package com.ziqing.apeletnews.retrofit;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.ObservableSource;
import io.reactivex.ObservableTransformer;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;

/**
 * 操作工具类
 * Created by pokermman on 2017/8/1.
 */

public class RetrofitUtil {
    /**
     * 构造方法私有
     */
    private RetrofitUtil() {
    }

    /**
     * 在访问HttpUtil时创建单例
     */
    private static class SingletonHolder {
        private static final RetrofitUtil INSTANCE = new RetrofitUtil();
    }

    /**
     * 获取单例
     */
    public static RetrofitUtil getInstance() {
        return SingletonHolder.INSTANCE;
    }

    public void toSubscribe(Observable ob, final ProgressObserver subscriber,
                            String cacheKey,
                            ObservableTransformer bindToLifecycle, Boolean isRefresh) {
        ObservableTransformer<HttpResult<Object>, Object> result = handleResult(bindToLifecycle);
//        //重用操作符
//        Observable observable = ob.compose(result)
//                .doOnSubscribe(new Action0() {
//                    @Override
//                    public void call() {
//                        //显示Dialog和一些其他操作
//                        subscriber.showProgressDialog();
//                    }
//                });
        Observable observable = ob.compose(result)
                .doOnSubscribe(new Consumer<Disposable>() {
                    @Override
                    public void accept(Disposable disposable) throws Exception {
                        subscriber.showProgressDialog();
                    }
                });
        RetrofitCache.load(cacheKey, observable, true, isRefresh).subscribe(subscriber);
//        observable.subscribe(subscriber);
    }


    /**
     * 预处理数据
     */
    public static <T> ObservableTransformer<HttpResult<T>, T> handleResult(final ObservableTransformer<T, T> bindToLifecycle) {
        return new ObservableTransformer<HttpResult<T>, T>() {
            @Override
            public ObservableSource<T> apply(Observable<HttpResult<T>> upstream) {
                return upstream.flatMap(new Function<HttpResult<T>, ObservableSource<T>>() {
                    @Override
                    public ObservableSource<T> apply(HttpResult<T> httpResult) throws Exception {
                        if (httpResult.getStatus() == 1) {
                            return createData(httpResult.getContent());
                        } else {
                            return Observable.error(new ApiException(httpResult));
                        }
                    }
                }).compose(bindToLifecycle)
                        .compose(RxSchedulers.<T>io_main());
            }
        };
    }

    public static <T> Observable<T> createData(final T t) {
        return Observable.create(new ObservableOnSubscribe<T>() {

            public void subscribe(ObservableEmitter<T> subscriber) throws Exception {
                try {
                    subscriber.onNext(t);
                    subscriber.onComplete();
                } catch (Exception e) {
                    subscriber.onError(e);
                }


            }
        });
    }


}
